package com.wangjie;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangjie.mapper.UserMapper;
import com.wangjie.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jieshao
 * @date 2022/2/10 16:49
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class)
public class UserTest01 {
    @Autowired
    private UserMapper userMapper;

    @Test
    public void test01() {
        List<User> list = userMapper.selectList(null);
        list.forEach(System.out::println);
    }

    @Test
    public void test02() {
        User user = new User();
        user.setName("lucy");
        user.setAge(20);
        user.setEmail("1234@qq.com");
        int insert = userMapper.insert(user);
        System.out.println(insert);
    }

    @Test
    public void test03() {
        User user = new User();
        user.setId(3L);
        user.setName("Tomcat");
        int i = userMapper.updateById(user);
        System.out.println(i);
    }

    @Test
    public void test04() {
        User user1 = userMapper.selectById(2);
        User user2 = userMapper.selectById(2);

        user1.setAge(25);
        user2.setAge(26);

        int i1 = userMapper.updateById(user1);
        int i2 = userMapper.updateById(user2);

        System.out.println(i1);
        System.out.println(i2);
    }

    @Test
    public void test05() {
        // 通过多个id批量查询
        List<User> list = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
        list.forEach(System.out::println);
    }

    @Test
    public void test06() {
        // 通过map封装查询条件，map中的key对应数据库中的列名
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("name", "Jone");
        columnMap.put("age", 18);
        List<User> list = userMapper.selectByMap(columnMap);
        list.forEach(System.out::println);
    }

    @Test
    public void test07() {
        // 分页查询
        Page<User> page = new Page<>(1, 2);
        Page<User> userPage = userMapper.selectPage(page, null);
        List<User> list = userPage.getRecords();
        long current = userPage.getCurrent(); // 当前页
        long pages = userPage.getPages(); // 总页数
        long total = userPage.getTotal(); // 总记录数
        boolean hasPrevious = userPage.hasPrevious(); // 上一页
        boolean hasNext = userPage.hasNext(); // 下一页

        list.forEach(System.out::println);

        System.out.println("当前页: " + current);
        System.out.println("总页数: " + pages);
        System.out.println("总记录数: " + total);
        System.out.println("上一页: " + hasPrevious);
        System.out.println("下一页: " + hasNext);
    }

    @Test
    public void test08() {
        // 据id删除
        int i = userMapper.deleteById(1L);
        System.out.println(i);
    }

    @Test
    public void test09() {
        // 批量删除
        int i = userMapper.deleteBatchIds(Arrays.asList(2, 3));
        System.out.println(i);
    }

    @Test
    public void test10() {
        // 简单条件删除
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("name", "Sandy");
        columnMap.put("age", 21);
        int i = userMapper.deleteByMap(columnMap);
        System.out.println(i);
    }
}